package com.jinzhi.jzweb.controller;


import java.math.BigDecimal;
import java.util.*;

import com.jinzhi.api.util.JWTUtil;
import com.jinzhi.jzweb.domain.ReserveStoreCommodityDO;
import com.jinzhi.jzweb.service.ReserveStoreCommodityService;
import lombok.experimental.var;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.ReserveShoppingCartDO;
import com.jinzhi.jzweb.service.ReserveShoppingCartService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 商品预定购物车
 * </pre>
 * <small> 2023-08-17 10:49:35 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/reserveShoppingCart")
public class ReserveShoppingCartController extends BaseController {
	@Autowired
	private ReserveShoppingCartService reserveShoppingCartService;

	@Autowired
	private ReserveStoreCommodityService reserveStoreCommodityService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:reserveShoppingCart:reserveShoppingCart")
	String ReserveShoppingCart(){
	    return "jzweb/reserveShoppingCart/reserveShoppingCart";
	}



	@Log("查看购物车商品")
	@ResponseBody
	@RequestMapping("/list")
	public Result<List<ReserveShoppingCartDO>> list(){
//		Long userId = this.getUserId();
		Long userId = 1504759468294201346L;
		EntityWrapper<ReserveShoppingCartDO> wrapper =new EntityWrapper();
		wrapper.eq("user_id",userId);
		List<ReserveShoppingCartDO> list = reserveShoppingCartService.selectList(wrapper);
		for (int i = 0;i<list.size();i++){
			ReserveStoreCommodityDO reserveStoreCommodityDO = reserveStoreCommodityService.selectById(list.get(i).getCommodityId());
			list.get(i).setCommodityName(reserveStoreCommodityDO.getCommodityName());
			list.get(i).setCommodityPrice(String.valueOf(reserveStoreCommodityDO.getCommodityPrices().multiply(new BigDecimal(list.get(i).getQuantity()))));
		}
		return Result.ok(list);
	}




	@Log("添加商品到购物车")
	@ResponseBody
	@PostMapping("/save")
	public Result<String>  save( ReserveShoppingCartDO reserveShoppingCart){
		reserveShoppingCart.setUserId(1504759468294201346L);
//		reserveShoppingCart.setUserId(this.getUserId());
		reserveShoppingCartService.insert(reserveShoppingCart);
		return Result.ok();
	}
	

	
	@Log("修改商品预定购物车")
	@ResponseBody
	@PostMapping("/update")
	public Result<String>  update(String id,String quantity){
		ReserveShoppingCartDO reserveShoppingCartDO = reserveShoppingCartService.selectById(id);
		reserveShoppingCartDO.setQuantity(Integer.valueOf(quantity));
		boolean update = reserveShoppingCartService.updateById(reserveShoppingCartDO);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除商品预定购物车")
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Long id){
		boolean b = reserveShoppingCartService.deleteById(id);
		return b ? Result.ok() : Result.fail();
	}

	
}
